/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.tool;

import codegener.entity.TableInfo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

/**
 *
 * @author 32181
 */
public class DBGet {
    
    
    private static BasicDataSource ds=null;
    private static Connection conn=null;
    private static QueryRunner qr=null;
    private static String type;
    
    private static DMTabGet dmTabGet=new DMTabGet();
    private static OracleTabGet oracleTabGet=new OracleTabGet();
    private static MySQLTabGet mysqlTabGet=new MySQLTabGet();
    
    public static void initDs(String type,String url,String uname,String upass){
        String driver=null;
        if(null!=conn){
            try{
                DbUtils.closeQuietly(conn);
                conn=null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        switch(type){
            case "MySQL":{
                try{
                    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                    driver="com.mysql.jdbc.Driver";
                }catch(Exception e){
                    e.printStackTrace();
                }
            }break;
            case "Oracle":{
                try{
                    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                    driver="oracle.jdbc.driver.OracleDriver";
                }catch(Exception e){
                    e.printStackTrace();
                }
            }break;
            case "达梦":{
                try{
                    DriverManager.registerDriver(new dm.jdbc.driver.DmDriver());
                    driver="dm.jdbc.driver.DmDriver";
                }catch(Exception e){
                    e.printStackTrace();
                }
            }break;
            case "dm":{
                try{
                    DriverManager.registerDriver(new dm.jdbc.driver.DmDriver());
                    driver="dm.jdbc.driver.DmDriver";
                }catch(Exception e){
                    e.printStackTrace();
                }
            }break;
        }
        if(null==conn){
            try {
                conn=DriverManager.getConnection(url, uname, upass);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            DBGet.type=type;
        }
        
    }
    
    public static Connection getConn(){
        try{
            if(DBGet.conn.isValid(3000)){
                return DBGet.conn;
            }
        }catch(Exception e){
            
        }
        return DBGet.conn;
    }
    
    public static QueryRunner getQR(){
        if(null==qr){
            qr=new QueryRunner();
        }
        return qr;
    }
    
    public static List<String> listTables(){
         try{
            if(null==DBGet.type){
                return null;
            }
            switch(DBGet.type){
                case "MySQL":{
                     return mysqlTabGet.listTables();
                }
                case "Oracle":{
                    return oracleTabGet.listTables();
                }
                case "达梦":{
                    return dmTabGet.listTables();
                }
                case "dm":{
                    return dmTabGet.listTables();
                }
                default :{
                };
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public static List<String> listTables(String likeName){
         try{
            switch(DBGet.type){
                case "MySQL":{
                   return mysqlTabGet.listTables(likeName);
                }
                case "Oracle":{
                    return oracleTabGet.listTables(likeName);
                }
                case "达梦":{
                    return dmTabGet.listTables(likeName);
                }
                case "dm":{
                    return dmTabGet.listTables(likeName);
                }
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public static List<TableInfo> getTable(String...tableNames){
        try{
            switch(DBGet.type){
                case "MySQL":{
                    return mysqlTabGet.getTable(tableNames);
                }
                case "Oracle":{
                      return oracleTabGet.getTable(tableNames);
                }
                case "达梦":{
                       return dmTabGet.getTable(tableNames);
                }
                case "dm":{
                    return dmTabGet.getTable(tableNames);
                }
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    } 
    /**
     * 获取表数据量统计
     * @param tableName
     * @return 
     */
    public static List<TableInfo> getTableDat(String tableName){
        try{
            switch(DBGet.type){
                case "MySQL":{
                   return mysqlTabGet.tableDat(tableName);
                }
                case "Oracle":{
                      return oracleTabGet.tableDat(tableName);
                }
                case "达梦":{
                       return dmTabGet.tableDat(tableName);
                }
                case "dm":{
                    return dmTabGet.tableDat(tableName);
                }
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    } 
}
